﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace AppRPG
{
    public partial class MainForm : Form
    {
        private readonly List<ToolStripMenuItem> _toolStripMenuItems;
        private readonly List<Button> _buttons;
        private readonly List<Label> _labels;
        private readonly List<TextBox> _textBoxes; 

        private const int GameTimerInterval = 10;

        private readonly Game _game;


        public MainForm()
        {
            InitializeComponent();

            _game = new Game();

            _toolStripMenuItems = new List<ToolStripMenuItem>();
            _toolStripMenuItems.AddRange(new List<ToolStripMenuItem> { файлToolStripMenuItem, редакторToolStripMenuItem, выходToolStripMenuItem });
            _buttons = new List<Button>();
            _buttons.AddRange(new List<Button> { buttonChoose, buttonChangeObject, buttonChooseTextureObject, buttonDeleteObject });
            _labels = new List<Label>(){label1, label2, label3, labelNameObject, labelPropertiesObject};
           _textBoxes = new List<TextBox>(){textBoxHeightObject, textBoxNameObject, textBoxNameTextureObject, textBoxPosXObject, textBoxPosYObject, textBoxWidthObject};

            const int outRange = -1000;
            buttonChoose.Location = new Point(outRange, outRange);
        
            GameTimer.Interval = GameTimerInterval;
            GameTimer.Tick += Update;

            Paint += Draw;
            DoubleBuffered = true;

            GameTimer.Enabled = true;
            GameTimer.Start();
        }

        private float GetDeltaTime()
        {
            // TODO: Need delta time.

            return 0;
        }

        private void Update(object sender, EventArgs e)
        {
            _game.Update(0f);

            Invalidate();
        }

        private void Draw(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;

            if (!_game.MapEditor.IsOn)
            {
                _game.Draw(e.Graphics);
            }
            _game.MapEditor.Draw(e.Graphics, buttonChoose);
            _game.MapEditor.ShowInfo(textBoxNameObject, textBoxPosXObject, textBoxPosYObject,
                                                        textBoxWidthObject, textBoxHeightObject, textBoxNameTextureObject);
            ShowInfoEditor(_game.MapEditor.IsOn);
        }

        private void ShowInfoEditor(bool flag)
        {
            foreach (var toolStripMenuItem in _toolStripMenuItems)
            {
                toolStripMenuItem.Visible = flag;
            }
            foreach (var button in _buttons)
            {
                button.Visible = flag;
            }

            foreach (var label in _labels)
            {
                label.Visible = flag;
            }
            foreach (var textBox in _textBoxes)
            {
                textBox.Visible = flag;
            }
            menuStrip1.Visible = flag;
        }

        private void показатьДоступныеИгровыеОбъектыToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _game.MapEditor.ShowAllObjects = !_game.MapEditor.ShowAllObjects;
        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Shift)
            {
                _game.MapEditor.IsOn = !_game.MapEditor.IsOn;
                Refresh();
            }
        }

        private void MainForm_MouseClick(object sender, MouseEventArgs e)
        {
            _game.MapEditor.ReactionClick(e);
        }

        private void buttonChoose_Click(object sender, EventArgs e)
        {
            _game.MapEditor.AcceptChoose();
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            _game.MapEditor.CheckMouseMove(e.Location);
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {

            Refresh();
        }

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            _game.MapEditor.SaveClick(e);
        }

        private void MainForm_MouseUp(object sender, MouseEventArgs e)
        {
            _game.MapEditor.PlaceMovingGameObject();
            _game.MapEditor.CancelClick();
        }

        private void buttonChangeObject_Click(object sender, EventArgs e)
        {
            _game.MapEditor.SetInfoObject(textBoxNameObject, textBoxPosXObject, textBoxPosYObject,
                                                            textBoxWidthObject, textBoxHeightObject);
        }

        private void buttonDeleteObject_Click(object sender, EventArgs e)
        {
            _game.MapEditor.DeleteObject();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void buttonChooseTextureObject_Click(object sender, EventArgs e)
        {
            _game.MapEditor.SetTextureLastGameObject();
        }

        private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _game.MapEditor.Save();
        }

        private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _game.MapEditor.Open();
        }

        private void buttonModeRedactor_Click(object sender, EventArgs e)
        {
            _game.MapEditor.IsOn = !_game.MapEditor.IsOn;
            if (!_game.MapEditor.IsOn)
                buttonModeRedactor.Text = "Режим  редактора";
            else
            {
                buttonModeRedactor.Text = "Режим игры";
            }
            _game.GetObjectsFromEditor();
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            const int factor = 10;
            const int factor1 = 15;
            foreach (var button in _buttons)
            {
                button.Location = new Point(Width - Width / factor, button.Location.Y);
            }

            foreach (var label in _labels)
            {
                label.Location = new Point(Width - Width / factor, label.Location.Y);
            }

            foreach (var textbox in _textBoxes)
            {
                textbox.Location = new Point(Width - Width / factor, textbox.Location.Y);
            }
            textBoxHeightObject.Location = new Point(Width - Width / factor1, textBoxHeightObject.Location.Y);
            textBoxPosYObject.Location = new Point(Width - Width / factor1, textBoxPosYObject.Location.Y);

            buttonChooseTextureObject.Location = new Point(textBoxNameTextureObject.Location.X + 100, buttonChooseTextureObject.Location.Y);

        }       
    }
}
